package com.diseaseInformation

class ManagementController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    
    telemedicine.MyformatToHtmlService myformatToHtmlService
  
    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [managementInstanceList: Management.list(params), managementInstanceTotal: Management.count()]
    }

    def create = {
        def managementInstance = new Management()
        managementInstance.properties = params
        return [managementInstance: managementInstance]
    }

    def save = {
        def managementInstance = new Management(params)
        if (managementInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'management.label', default: 'Management'), managementInstance.id])}"
            redirect(action: "show", id: managementInstance.id)
        }
        else {
            render(view: "create", model: [managementInstance: managementInstance])
        }
    }

    def show = {
            myformatToHtmlService.managementviewverinc(params.id)
            def managementInstance = Management.read(params.id)
        if (!managementInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'management.label', default: 'Management'), params.id])}"
            redirect(action: "list")
        }
        else {
             managementInstance.data = myformatToHtmlService.toHtmlTagWebboard(managementInstance.data)
            [managementInstance: managementInstance]
        }
    }
    
    def edit = {
        def managementInstance = Management.get(params.id)
        if (!managementInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'management.label', default: 'Management'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [managementInstance: managementInstance]
        }
    }

    def update = {
        def managementInstance = Management.get(params.id)
        if (managementInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (managementInstance.version > version) {
                    
                    managementInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'management.label', default: 'Management')] as Object[], "Another user has updated this Management while you were editing")
                    render(view: "edit", model: [managementInstance: managementInstance])
                    return
                }
            }
            managementInstance.properties = params
            if (!managementInstance.hasErrors() && managementInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'management.label', default: 'Management'), managementInstance.id])}"
                redirect(action: "show", id: managementInstance.id)
            }
            else {
                render(view: "edit", model: [managementInstance: managementInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'management.label', default: 'Management'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def managementInstance = Management.get(params.id)
        if (managementInstance) {
            try {
                managementInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'management.label', default: 'Management'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'management.label', default: 'Management'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'management.label', default: 'Management'), params.id])}"
            redirect(action: "list")
        }
    }
    
    // mobile
    def mshow = {
        myformatToHtmlService.managementviewverinc(params.id)
        def managementInstance = Management.read(params.id)
        if (!managementInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'management.label', default: 'Management'), params.id])}"
            redirect(action: "list")
        }
        else {
             managementInstance.data = myformatToHtmlService.toHtmlTagMobile(managementInstance.data)
            [managementInstance: managementInstance]
        }
    }
    
    def mlist = {
        //params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [managementInstanceList: Management.list()]
    }
}
